package com.yq.core.util;

import java.sql.*;

/**
 * JDBC
 */

public class JDBC {

    public void insertSQL(String sql) {
        try {
            /*String URL = "jdbc:mysql://rm-2ze3nyqkl492o8l8xo.mysql.rds.aliyuncs.com:3306/cxtxdbdev?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8";
            String USER = "cxtxdev";
            String PASSWORD = "Admin@cxtxDB";*/

            String URL = "jdbc:mysql://localhost:3306/azeroth?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8";
            String USER = "root";
            String PASSWORD = "root";

            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");

            //2.获得数据库链接
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);

            //3.通过数据库的连接操作数据库，实现增删改查（使用Statement类）
            Statement st = conn.createStatement();

            int i=st.executeUpdate(sql);
            if(i==0){
                System.out.println(sql);
                System.out.println("失败");
            }

            /*ResultSet rs = st.executeQuery(sql);

            List<Map<String, Object>> list = Lists.newArrayList();
            ResultSetMetaData rsm = rs.getMetaData(); //获得列集

            //4.处理数据库的返回结果(使用ResultSet类)
            while (rs.next()) {
                Map<String, Object> obj = Maps.newConcurrentMap();
                for (int i = 1; i < rsm.getColumnCount() + 1; i++) {
                    String name = rsm.getColumnName(i);
                    obj.put(name, rs.getString(name));
                }
                list.add(obj);
            }*/

            //关闭资源
            //rs.close();
            st.close();
            conn.close();
        } catch (Exception e) {
            System.out.println(sql);
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {

        JDBC ew = new JDBC();
        ew.insertSQL("insert into magic (uid) values ('sssss')");

        System.out.println("================================================================");
    }
}
